/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_te_report_msg_data_req.c                         */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include <tongeasy/config.h>
#include <tongeasy/te_core.h>
#include <tongeasy/te_comm.h>
#include <tongeasy/te_packet.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include "../test_tongeasy_comm.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;

    CU_ASSERT_EQUAL(test_te_create_ct(&p_st_conn, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_te_private *p_st_session_private = (L7session_te_private *) frct_get_session_private(p_st_session);
    L7_te_packet *p_st_te_packet = p_st_session_private->p_st_te_packet;

    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;
    int i_payload_offset = 0;

    CU_ASSERT_EQUAL(te_packet_unfragment(p_st_packet, p_st_te_packet, i_payload_offset), APM_TE_CHECK_FRAGMENT_SUCCESS);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    p_st_body = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, "test"), FR_SUCC);

    CU_ASSERT_EQUAL(te_report_msg_data_req(p_st_conn, p_st_te_packet, p_st_body, &st_hash_report), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    unsigned int ui_loop = 0;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    FRHASH_LOOP(ui_loop, &st_hash_report, p_list_report)
    {
        if (p_list_report == NULL)
            continue;
        p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
        while (p_st_node_report)
        {
            p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
            if (p_st_report_data)
            {
                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->p_field_name, "transaction.title.s");
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "test");
            }
            p_st_node_report = FRLIST_NEXT_NODE(p_st_node_report);
        }
    }

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_te_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_te_report_msg_data_req(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" te_report_msg_data_req()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
